import requests, json, time, datetime
from requests.auth import HTTPBasicAuth
import sys
username = 'anyuan'
password = 'anyuan'
queue_url = 'http://122.112.235.59:15672/api/queues'
connections = 'http://122.112.235.59:15672/api/connections'
channels = 'http://122.112.235.59:15672/api/channels'
consumers_url = 'http://122.112.235.59:15672/api/consumers'
overview_url = 'http://122.112.235.59:15672/api/overview'
erlang_url = 'http://122.112.235.59:15672/api/nodes/rabbit@ecs-pageoffice'
headers = {
	'User-Agent': "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/22.0.1207.1 Safari/537.1"};
key=sys.argv[1]



def monitor( key ):
    #连接数
    if key == 'connectionsnum':
        res = requests.get(url=connections, auth=HTTPBasicAuth(username=username, password=password), timeout=5)
        num = 0
        for vhost in json.loads(res.text):
            name=vhost.get('vhost')
            if name != "":
                num=num + 1
        print(num)
    #channel总数
    elif key == 'channelsnum':
        res = requests.get(url=connections, auth=HTTPBasicAuth(username=username, password=password), timeout=5)
        num = 0
        for  channels in json.loads(res.text):
            channelnum = channels.get('channels')
            num=num+int(channelnum)
        print(num)
    #队列数目
    elif key == 'queuenum':
        res = requests.get(url=queue_url, auth=HTTPBasicAuth(username=username, password=password), timeout=5)
        num = 0
        for queue1 in json.loads(res.text):
            name = queue1.get('name')
            if name != "":
                num=num+1
        print(num)
    #消费者数目
    elif key == 'consumersnum':
        res = requests.get(url=consumers_url, auth=HTTPBasicAuth(username=username, password=password), timeout=5)
        num = 0
        for consumers in json.loads(res.text):
            id = consumers.get('consumer_tag')
            if id != "":
                num=num+1
        print(num)
    #消息确认速率
    elif key == 'acknowledge':
        res = requests.get(url=overview_url, auth=HTTPBasicAuth(username=username, password=password), timeout=5)
        acknowledge=json.loads(res.text).get('message_stats').get('deliver_details').get('rate')
        print(acknowledge)
    #消息发布速率
    elif key == 'publish':
        res = requests.get(url=overview_url, auth=HTTPBasicAuth(username=username, password=password), timeout=5)
        publish=json.loads(res.text).get('message_stats').get('publish_details').get('rate')
        print(publish)
    #消息传送速率
    elif key == 'deliver':
        res = requests.get(url=overview_url, auth=HTTPBasicAuth(username=username, password=password), timeout=5)
        deliver=json.loads(res.text).get('message_stats').get('deliver_get_details').get('rate')
        print(deliver)
    #消息获取速率
    elif key == 'get':
        res = requests.get(url=overview_url, auth=HTTPBasicAuth(username=username, password=password), timeout=5)
        get=json.loads(res.text).get('message_stats').get('get_details').get('rate')
        print(get)
    #已经使用的erlang进程
    elif key == 'erlangpid':
        res = requests.get(url=erlang_url, auth=HTTPBasicAuth(username=username, password=password), timeout=5)
        erlangpid=json.loads(res.text).get('proc_used')
        print(erlangpid)
    #erlang进程数目限制
    elif key == 'erlangpidtotal':
        res = requests.get(url=erlang_url, auth=HTTPBasicAuth(username=username, password=password), timeout=5)
        erlangpidtotal=json.loads(res.text).get('proc_total')
        print(erlangpidtotal)
    #memory使用
    elif key == 'mem_used':
        res = requests.get(url=erlang_url, auth=HTTPBasicAuth(username=username, password=password), timeout=5)
        mem_used=json.loads(res.text).get('mem_used')
        memused=int(mem_used/1024/1024)
        print(memused)
    #sockets已使用
    elif key == 'socketsused':
        res = requests.get(url=erlang_url, auth=HTTPBasicAuth(username=username, password=password), timeout=5)
        socketsused=json.loads(res.text).get('sockets_used')
        print(socketsused)
    #消息ready数目
    elif key == 'readynum':
        getMessageCount('readynum')
    #消息unacked数目
    elif key == 'unackednum':
        getMessageCount('unackednum')
    #消息总数目
    elif key == 'total':
        getMessageCount('total')
    else:
        print("error")

def getMessageCount( key ):
    url =  'http://122.112.235.59:15672/api/queues'
    r = requests.get(url=queue_url, auth=HTTPBasicAuth(username=username, password=password))
    if r.status_code != 200:
        return -1
    dic = json.loads(r.text)
    readynum = 0
    unackednum = 0
    totalnum = 0
    name = ""
    for item in dic:
        ready, unacked, total, name = item['messages_ready'], item['messages_unacknowledged'], item['messages'], item[
            'name']
        if ready >= 0 or unacked >= 0 or total >= 0:
            msg = """
                         ready:%d
                         unacked:%d
                         total:%d
                         queryName:%s
                         """ % (ready, unacked, total, name)
            if ready >= 1:
                print(name)
            readynum = readynum + ready
            unackednum = unackednum + unacked
            total = totalnum + total
#            if ready > 0 or unacked > 0:
#                print(name)
    if key == 'readynum':
        print(readynum)
    elif key == 'unackednum':
        print(unackednum)
    elif key == 'total':
        print(total)
    else:
        print("error")
#           print(name)
#        print('ready: %d' % ready)
#        print('unacked: %d' % unacked)
#        print('total: %d' % total)
#        print('name: %s' % name)
    # return dic['messages_ready'], dic['messages_unacknowledged'], dic['messages']
if __name__ == '__main__':
    monitor(key)
#    getMessageCount(key)